package com.sk.photo_album.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.Glide;
import com.sk.photo_album.databinding.AdapterSpinnerBinding;

import java.util.ArrayList;
import java.util.List;

public class SpinnerAdapter extends BaseAdapter {

    protected List<String> mBeans;

    public void reloadData(List<String> beans) {
        if (mBeans == null) {
            mBeans = new ArrayList<>();
        } else {
            mBeans.clear();
        }

        if (beans != null && beans.size() > 0) {
            mBeans.addAll(beans);
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mBeans != null ? mBeans.size() : 0;
    }

    public List<String> getData() {
        return mBeans == null ? new ArrayList<>() : mBeans;
    }

    @Override
    public String getItem(int position) {
        return mBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            AdapterSpinnerBinding binding = AdapterSpinnerBinding.inflate(LayoutInflater.from(parent.getContext()));
            convertView = binding.getRoot();
            convertView.setTag(new Holder(binding));
        }

        ((Holder) convertView.getTag()).setData(mBeans.get(position));

        return convertView;
    }

    static class Holder {
        AdapterSpinnerBinding binding;

        public Holder(AdapterSpinnerBinding binding) {
            this.binding = binding;
        }

        public void setData(String str) {
            binding.text.setText(str);
        }
    }
}
